package net.mattiraty.imagebrowser;

import java.net.URISyntaxException;

import android.app.Activity;
import android.content.Context;
import android.net.ContentURI;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;

/**
 * Activity that displays thumbnails at the bottom of 
 * the view and selected image in fullscreen.
 * 
 * Pretty much rip off from Android SDK sources. Only
 * difference is that this activity can display
 * picture files.
 *
 */
public class ThumbnailView extends Activity implements
	AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
	
		public static final int ACTIVITY_VIEW = 0;
		
		public static final String BUNDLE_EXTRA_FILELIST = 
			"net.mattiraty.imagebrowser.FileList";
		
		
		private String TAG = "ThumbnailView";
		
	    private ImageSwitcher mSwitcher;
	    
	    private ContentURI[] mUrls; 
	    	   

	    @Override
	    public void onCreate(Bundle icicle) {
	        super.onCreate(icicle);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        
	        Bundle extras = getIntent().getExtras();
	        
	        /* Lets get those file paths and
	         * convert them into ContentURIs
	         */
	        String[] fileList = extras.getStringArray(BUNDLE_EXTRA_FILELIST);
	        
	        mUrls = new ContentURI[fileList.length];
	        for(int i=0; i < fileList.length; i++) {
	        	try {
	        		mUrls[i] = new ContentURI(fileList[i]);	        		
	        	}
	        	catch(URISyntaxException e) {
	        		/* Ok, this is not error handling,
	        		 * it is more like error ignoring.
	        		 * Do you agree, Simo :)
	        		 */
	        		Log.e(TAG, "Invalid content URI");
	        	}	        	
	        }
	        	      
	        setContentView(R.layout.thumbnail_view);
	        
	        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
	        mSwitcher.setFactory(this);
	        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
	                android.R.anim.fade_in));
	        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
	                android.R.anim.fade_out));

	        
	        Gallery g = (Gallery) findViewById(R.id.gallery);
	        g.setAdapter(new ImageAdapter(this));
	        g.setSelectorSkin(getResources().getDrawable(android.R.drawable.box));
	        g.setOnItemSelectedListener(this);
	        
	        if(mUrls.length > 0 ) {
	        	mSwitcher.setImageURI(mUrls[0]);
	        }
	    }

	    public void onItemSelected(AdapterView parent, View v, int position, long id) {	     
	    	mSwitcher.setImageURI(mUrls[position]);
	    }

	    public void onNothingSelected(AdapterView parent) {
	    }

	    public View makeView() {
	    	ImageView i = new ImageView(this);
	        i.setBackgroundColor(0xFF000000);
	        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
	        i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	                LayoutParams.FILL_PARENT));
	        return i;
	    }

	    /**
	     * Image adapter for thumbnail list
	     *
	     */
	    public class ImageAdapter extends BaseAdapter {
	    	
	        private Context mContext;
	        
	        public ImageAdapter(Context c) {
	            mContext = c;
	        }

	        public int getCount() {
	            return mUrls.length;	        	
	        }

	        public Object getItem(int position) {
	            return position;
	        }

	        public long getItemId(int position) {
	            return position;
	        }

	        public View getView(int position, View convertView, ViewGroup parent) {
	            ImageView i = new ImageView(mContext);
	            
	            i.setImageURI(mUrls[position]);
	            i.setAdjustViewBounds(true);
	            i.setLayoutParams(new Gallery.LayoutParams(
	                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	            i.setBackground(android.R.drawable.picture_frame);
	            return i;
	        }

	        public float getAlpha(boolean focused, int offset) {
	            return Math.max(0.2f, 1.0f - (0.2f * Math.abs(offset)));
	        }

	        public float getScale(boolean focused, int offset) {
	            return Math.max(0, offset == 0 ? 1.0f : 0.6f);
	        }

	    }
	}

